//
//  SymptomsBtnView.m
//  CareRingApp
//
//  Created by Linktop on 2024/4/7.
//

#import "SymptomsBtnView.h"

@implementation SymptomsBtnView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self layoutBase];
    }
    return self;
}

-(void)layoutBase {
    
    UIView *line = [UIView new];
    line.backgroundColor = SEPLINE_COLOR(1);
    [self addSubview: line];
    
    [line mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self);
        make.height.equalTo(@0.5);
    }];

    [self addSubview: self.titlelbl];
    [self addSubview: self.symptomsLbl];
    [self addSubview: self.selectBtn];
    
    [self.titlelbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.titlelbl.superview.mas_leading);
        make.top.equalTo(line.mas_bottom).offset(3);
        
    }];
    [self.symptomsLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.titlelbl.mas_leading);
        make.height.greaterThanOrEqualTo(@15);
        make.trailing.equalTo(self.symptomsLbl.superview.mas_trailing);
        make.top.equalTo(self.titlelbl.mas_bottom).offset(5);
    }];
    
    [self.selectBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.titlelbl.mas_leading);
        make.top.equalTo(self.symptomsLbl.mas_bottom).offset(2);
        make.height.equalTo(@35);
        
        make.bottom.equalTo(self.mas_bottom);
    }];

}

-(void)reset
{
    self.symptomsArray = nil;
}

-(void)setSymptomsArray:(NSMutableArray<NSNumber *> *)symptomsArray
{
    _symptomsArray = symptomsArray;
    self.selectBtn.selected =  _symptomsArray.count > 0;
    [self freshSymptomsDesc];
    
}

-(void)freshSymptomsDesc {
    if (!self.symptomsArray.count) {
        self.symptomsLbl.attributedText = nil;
        return;
    }
    // 添加圆点
    NSString *bulletPoint = @"\u2022";
    NSString *bulletPointString = [NSString stringWithFormat:@"%@ ", bulletPoint];
    NSDictionary *bulletPointAttribute = @{NSParagraphStyleAttributeName: [NSParagraphStyle defaultParagraphStyle], NSFontAttributeName: [UIFont systemFontOfSize:14]};
    NSAttributedString *bulletPointAttributedString = [[NSAttributedString alloc] initWithString:bulletPointString attributes:bulletPointAttribute];
    
    
    NSMutableAttributedString *totalAttreiString = [NSMutableAttributedString new];
    [self.symptomsArray enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *titleString = [NSString stringWithFormat:@"%@\r\n", [DBEcgRecord titleOfSymptom:obj.integerValue]];
        
        // 创建一个NSMutableAttributedString
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titleString];
        // 将圆点添加到文字前面
        [attributedString insertAttributedString:bulletPointAttributedString atIndex:0];
        [totalAttreiString appendAttributedString:attributedString];
    }];
  
    self.symptomsLbl.attributedText = totalAttreiString;
    [self layoutIfNeeded];
    
}



-(void)btnClick:(QMUIButton *)btn {
    
    if (self.clickBlk) {
        self.clickBlk();
    }
}

-(UILabel *)titlelbl
{
    if (!_titlelbl) {
        _titlelbl = [UILabel new];
        _titlelbl.text = _L(L_TITL_NOT_FEEL_WELL);
    }
    return _titlelbl;
}

-(UILabel *)symptomsLbl
{
    if (!_symptomsLbl) {
        _symptomsLbl = [UILabel new];
        _symptomsLbl.numberOfLines = 0;
    }
    return _symptomsLbl;
}

-(QMUIButton *)selectBtn
{
    if (!_selectBtn) {
        _selectBtn = [[QMUIButton alloc]init];
        [_selectBtn setImage:[UIImage imageNamed:@"ecg_add"] forState:UIControlStateNormal];
        [_selectBtn setImage:[UIImage imageNamed:@"edit"] forState:UIControlStateSelected];

        [_selectBtn setTitle:_L(L_ADD_SYMP) forState:UIControlStateNormal];
        [_selectBtn setTitle:_L(L_EDIT_SYMP) forState:UIControlStateSelected];
        _selectBtn.imagePosition = QMUIButtonImagePositionRight;
        _selectBtn.spacingBetweenImageAndTitle = 10;
        _selectBtn.contentEdgeInsets= UIEdgeInsetsMake(3, 10, 3, 10);
        _selectBtn.backgroundColor = UIColor.grayColor;
        [_selectBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_selectBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _selectBtn.cornerRadius = 2;
    }
    return _selectBtn;
}

@end
